package com.example.cookie;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ws
 * Date: 2024-03-02
 * Time: 16:02
 *
 * 接受 cookie
 */
@ResponseBody
@Controller
@RequestMapping("/cookie")
public class CookieController {

    // 取 cookie
    @RequestMapping(value = "/data")
    public String data(@CookieValue("cookieName") String value) {
        System.out.println("value:" + value);
        return value;
    }


    // 存 cookie
    @GetMapping("/save")
    public String save(HttpServletResponse response) {
        Cookie cookie = new Cookie("cookieName", "root");
        // 将 cookie 添加到响应中
        response.addCookie(cookie);
        return "ok";
    }
}
